﻿<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="160px" v-loading="formLoading" :rules="rules">
      <el-form-item label="试卷类型：" prop="paperType" required>
        <el-select v-model="form.paperType" placeholder="题型" clearable>
            <el-option v-for="item in paperTypeList" :key="item.key" :value="item.key" :label="item.value"></el-option>

        </el-select>
      </el-form-item>
     <el-form-item label="试卷名称："  prop="name" required>
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item :key="index" :label="'试题：'" prop="titleItems" required v-for="(titleItem,index) in form.titleItems">
        <el-input v-show= false v-model="titleItem.name" style="width: 80%"/>

        <el-button type="text" icon="el-icon-plus" class="link-left" style="margin-left: 20px" size="mini" @click="addQuestion(titleItem)">
          添加题目
        </el-button>

        <el-card class="exampaper-item-box" v-if="titleItem.questionItems.length!==0">
          <el-form-item :key="questionIndex" :label="'题目'+(questionIndex+1)+'：'"
                        v-for="(questionItem,questionIndex) in titleItem.questionItems" style="margin-bottom: 15px">
            <el-row>
              <el-col :span="23">
                <QuestionShow :qType="questionItem.questionType" :question="questionItem"/>
              </el-col>
              <el-col :span="1">
                <el-button type="text" size="mini" @click="titleItem.questionItems.splice(questionIndex,1)">删除
                </el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-card>
      </el-form-item>
      <el-form-item label="建议时长：" prop="suggestTime" required>
        <el-input v-model="form.suggestTime" placeholder="分钟"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="questionPage.showDialog"  width="70%">
      <el-form :model="questionPage.queryParam" ref="queryForm" :inline="true">

        <el-form-item label="题型：">
          <el-select v-model="questionPage.queryParam.questionType" clearable>
             <el-option
                v-for="item in this.questionTypelist"
                :key="item.key"
                :value="item.key"
                :label="item.value"
              ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题干：">
          <el-input v-model="questionPage.queryParam.title"  clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryForm">查询</el-button>
        </el-form-item>

    </el-form>
   <el-table v-loading="questionPage.listLoading" :data="questionPage.tableData" :row-key="getRowKeys"
                @selection-change="handleSelectionChange" border fit highlight-current-row style="width: 100%">
        <el-table-column :reserve-selection="true" type="selection" width="35"></el-table-column>
        <el-table-column prop="id" label="Id" width="60px"/>
        <el-table-column prop="questionType" label="题型" :formatter="questionTypeFormatter" width="70px"/>
        <el-table-column prop="shortTitle" label="题干" show-overflow-tooltip/>
      </el-table>
      <pagination v-show="questionPage.total>0" :total="questionPage.total"
                  :page.sync="questionPage.queryParam.pageIndex" :limit.sync="questionPage.queryParam.pageSize"
                  @pagination="search"/>
      <span slot="footer" class="dialog-footer">
          <el-button @click="questionPage.showDialog = false">取 消</el-button>
          <el-button type="primary" @click="confirmQuestionSelect">确定</el-button>
     </span>
    </el-dialog>
  </div>
</template>

<script>
import { pselect,edit,ppageList,changeStatus,deletePaper} from "@/api/xzs/examPaper";
import { select,pageList} from "@/api/xzs/question";
import Editor from '@/components/Editor';
import QuestionShow from '../question/components/Show'
export default {
  inject:['reload'],
   components: {
    Editor, QuestionShow
  },
  data() {
    return {
      form: {
        id: null,
        level: null,
        subjectId: null,
        paperType: null,
        name:null,
        limitDateTime: null,
        suggestTime: null,
        titleItems: [],
        score:null
      },
      questionTypelist:[
          { key: 1, value: '单选题'},
          {key: 2, value: '多选题'},
          {key: 3, value: '判断题'},
          {key: 5, value: '简答题'}
      ],
      paperTypeList:[{ key: 1, value: '正式考试' }, { key: 0, value: '课堂互动' }],
      formLoading: false,
      rules: {
         paperType: [
          { required: true, message: '请选择试卷类型', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入试卷名称', trigger: 'blur' }
        ],
        suggestTime: [
          { required: true, message: '请输入建议时长', trigger: 'blur' }
        ]
      },
      questionPage: {
        multipleSelection: [],
        showDialog: false,
        queryParam: {
          id: null,
          questionType: null,
          subjectId: null,
          pageIndex: 1,
          pageSize: 5
        },
        listLoading: true,
        tableData: [],
        total: 0
      },
      currentTitleItem: null
    }
  },
  created() {
    this.form.titleItems.push({
        name: "试题",
        questionItems: []
      })
    let id = this.$route.query.id
    let _this = this
    /*this.initSubject(function () {
      _this.subjectFilter = _this.subjects
    })*/
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      pselect(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }
  },
  methods: {
     questionTypeFormatter : function(row, column) {
				return row.questionType == 1 ? "单选题" : row.questionType == 2 ? "多选题" : row.questionType == 3 ? "判断题": row.questionType == 5 ? "简答题": "暂无";
      },
      submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          edit(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.$router.push('/exam/paper/index')

            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    getRowKeys(row) {
      return row.id
    },
    addTitle () {
      this.form.titleItems.push({
        name: null,
        questionItems: []
      })
    },
    addQuestion (titleItem) {
      this.currentTitleItem = titleItem
      this.questionPage.showDialog = true
      this.search()
    },
    removeTitleItem (titleItem) {
      this.form.titleItems.remove(titleItem)
    },
    removeQuestion (titleItem, questionItem) {
      titleItem.questionItems.remove(questionItem)
    },
    queryForm () {
      this.questionPage.queryParam.pageIndex = 1
      this.search()
    },
    confirmQuestionSelect () {
      console.log(this.form.titleItems)
      let _this = this
      this.questionPage.multipleSelection.forEach(q => {
        select(q.id).then(re => {
          _this.currentTitleItem.questionItems.push(re.response)
        })
      })
      console.log(this.form.titleItems)
      this.questionPage.showDialog = false
    },
     handleSelectionChange (val) {
      this.questionPage.multipleSelection = val
    },
    search () {
      //this.questionPage.queryParam.subjectId = this.form.subjectId
      this.questionPage.listLoading = true
      pageList(this.questionPage.queryParam).then(data => {
        const re = data.response
        this.questionPage.tableData = re.list
        this.questionPage.total = re.total
        this.questionPage.queryParam.pageIndex = re.pageNum
        this.questionPage.listLoading = false
      })
    },
    resetForm () {
      this.reload()
    }
  }
};
</script>
<style lang="scss">
  .exampaper-item-box {
    .q-title {
      margin: 0px 5px 0px 5px;
    }
  }
</style>
